<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="反馈类型" prop="opinionType">
        <el-select v-model="queryParams.opinionType" placeholder="请选择反馈类型C端/B端" clearable size="small">
          <el-option
            v-for="dict in opinionTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否处理" prop="solveState">
        <el-select v-model="queryParams.solveState" placeholder="请选择是否已经处理" clearable size="small">
          <el-option
            v-for="dict in solveStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['opinion:opinion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['opinion:opinion:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="opinionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="反馈来源" align="center" prop="opinionType">
        <template slot-scope="scope">
          <dict-tag :options="opinionTypeOptions" :value="scope.row.opinionType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="反馈人id" align="center" prop="subUserId" />-->
      <el-table-column label="反馈内容" align="center" prop="opinionContent" />
      <el-table-column label="是否已经处理" align="center" prop="solveState">
        <template slot-scope="scope">
          <dict-tag :options="solveStateOptions" :value="scope.row.solveState"/>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="solveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.solveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['opinion:opinion:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleSolve(scope.row)"
            v-hasPermi="['opinion:opinion:edit']"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 编辑处理意见 -->
    <el-dialog title="处理意见" :visible.sync="solveOpinion" width="800px" append-to-body>
      <el-form :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处理意见" prop="refuseMsg">
          <el-input v-model="form.solveContent" type="textarea" placeholder="请输入处理意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="submitSolveContent">确 定</el-button>
        <el-button @click="solveCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改意见反馈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="反馈来源" prop="opinionType">
          <el-select v-model="form.opinionType" placeholder="请选择反馈类型C端/B端">
            <el-option
              v-for="dict in opinionTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="反馈内容" prop="opinionContent">
          <el-input v-model="form.opinionContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="反馈图片">
          <ImageScan v-model="form.opinionImages" />
        </el-form-item>
        <el-form-item label="反馈视频" prop="opinionVideo" v-if="videosOpen">
          <VideoScan :list="videos" ref="videoScan" v-if="open"></VideoScan>
        </el-form-item>
        <el-form-item label="是否处理">
          <el-radio-group v-model="form.solveState">
            <el-radio
              v-for="dict in solveStateOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              disabled
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理时间" prop="solveTime">
          <el-date-picker clearable size="small"
            v-model="form.solveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处理时间" readonly>
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOpinion, getOpinion, logicDelOpinion, addOpinion, updateOpinion, exportOpinion,solve } from "@/api/opinion/opinion";

export default {
  name: "Opinion",
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 意见反馈表格数据
      opinionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      solveOpinion:false,
      // 反馈类型C端/B端字典
      opinionTypeOptions: [],
      // 是否已经处理字典
      solveStateOptions: [],
      //视频列表
      videos:[],
      videosOpen:true,
      //图片列表
      imgs:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        opinionType: null,
        subUserId: null,
        opinionContent: null,
        opinionImages: null,
        opinionVideo: null,
        solveState: null,
        solveTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
      this.getList();
      this.calHeight()
      this.getDicts("JY_BUSINESS_TYPE").then(response => {
        this.opinionTypeOptions = response.data;
      });
    this.getDicts("sys_yes_no").then(response => {
      this.solveStateOptions = response.data;
    });
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询意见反馈列表 */
    getList() {
      this.loading = true;
      listOpinion(this.queryParams).then(response => {
        this.opinionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //意见反馈处理取消按钮
    solveCancel() {
      this.solveOpinion = false
    },
    // 取消按钮
    cancel() {
      console.log('cancel')
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        opinionType: null,
        subUserId: null,
        opinionContent: null,
        opinionImages: null,
        opinionVideo: null,
        solveState: "0",
        solveUserId: null,
        solveTime: null,
        delState: null,
        delType: null,
        delUser: null,
        delTime: null,
        solveContent: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加意见反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOpinion(id).then(response => {
        this.form = response.data;
        this.videosOpen = true
        this.videos = response.data.opinionVideo.split(",")
        if(this.videos.length == 1){
          if(!this.videos[0].trim()){
            this.videosOpen = false
          }
        }
        this.open = true;
        this.title = "修改意见反馈";
        // this.$refs.videoScan.doCreate()
      });
    },
    handleSolve(row){
      this.reset();
      const id = row.id
      getOpinion(id).then(response => {
        this.form = response.data;
        this.solveOpinion = true
      });
    },
    submitSolveContent(){
      solve(this.form).then(response => {
        this.msgSuccess("提交成功");
        this.solveOpinion = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOpinion(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOpinion(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除意见反馈编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelOpinion(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有意见反馈数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOpinion(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
